<?php
namespace app\index\controller;

use app\common\controller\IndexBase;
use think\Session;
use think\Db;

/**
* 
*/
class User extends IndexBase
{
	
	public function index(){
        $status = input('param.status');
        $where['user_id'] = $this->userInfo["id"];
        switch ($status) {
            case 'que':
                $where['status'] = 1;
            break;
            case 'fahuo':
                $where['status'] = 2;
            break;
            case 'shouhuo':
                $where['status'] = 3;
            break;
            case 'wanchang':
                $where['status'] = 4;
            break;
            default:
                $status = 'all';
            break;
        }
        $list = Db::name("Up_goods")->where($where)->order('add_time desc')->select();
        $this->titleName = "我的订单";
        $user = Db::name('user')->alias("a")
            ->join("level b","a.level_id = b.id","LEFT")
            ->join("agent c","a.agent_id = c.id","LEFT")
            ->join("user_info d","a.id = d.user_id","LEFT")
            ->where('a.id',$this->userInfo["id"])->find();
        $num = count($list);
        $this->assign("status",$status);
        $this->assign("data",$list);
        $this->assign("num",$num);
        $this->assign("user",$user);
        $this->titleName = "个人中心";
        return $this->fetch();
	}

    //我的推广码
    public function my_code(){

        $uid = $this->userInfo["id"];

        $codeUrl = Db::name("User")->where("id",$uid)->value("code_url");
        if( empty($codeUrl) ){
            //邀请码
            $codeUrl = '/invited_code/'.md5(time().rand(1000,9999)).'.png';
            $filename = '.'.$codeUrl;
            $invited_code = $uid;
            invited_code($filename,$invited_code);
            Db::name("User")->where("id",$uid)->update(array("code_url"=>$codeUrl));
        }

        $this->assign("codeUrl",$codeUrl);
        $this->titleName = "我的推广码";
        return $this->fetch();
    }

	//修改密码
    function save_paw(){

        if( request()->isAjax() ){

            $map = input('post.');
            $v = Session::get('save_code');
            if(intval($map['save_code']) != $v){
                $retArr = [
                    "result" => 3,
                    "msg" => "你的验证码有误",
                ];
                $this->json_return($retArr);
            }

            Session::delete('save_code');

            $saveArr = [];
            $pas_salt = getRandChar(6);
            
            if( !empty($map["pwd"]) ){
                $saveArr["password"] = getMd5Pas($map["pwd"], $pas_salt);
                $saveArr["pwd_code"] = $pas_salt;
            }
            if( !empty($saveArr) ){
                $pd = Db::name("User")->where("id",$this->userInfo["id"] )->update($saveArr);
                if( $pd ){
                    $retArr = [
                        "result" => 0,
                        "msg" => "修改成功",
                    ];
                }else{
                    $retArr = [
                        "result" => 3,
                        "msg" => "修改失败",
                    ];
                }

            }else{
                $retArr = [
                    "result" => 1,
                    "msg" => "请输入你要修改的密码或则资金密码",
                ];
            }
            $this->json_return($retArr);
        }

        $Arr = Db::name("user")->where("id",$this->userInfo["id"])->column("phone");
        $this->assign("phone",$Arr[0]);
        return $this->fetch();
    }

    //获取验证码
    function save_code(){
        if( request()->isAjax() ){

            $phone = input('post.phone');
            $code = rand(100000,999999);
            //加载common目录下的TransmitSms
            $sms = app_service("common","TransmitSms");
            $pd = $sms->phone_validate($phone,$code);

            if($pd){
                Session::set('save_code',$code);
                $retArr = [
                    "result" => 0,
                    "msg" => "发送成功",
                ];
            }else{
                $retArr = [
                    "result" => 1001,
                    "msg" => "发送失败",
                ];
                // $this->dataError = "uid=".UID."|".json_encode($data);
            }
            $this->json_return($retArr);
        }
    }


    //修改个人信息
    public function edit_msg(){

        if( request()->isPost() ){

            $map = input("post.");
            print_r($map);
            $file = request()->file('upfile');

            if($file){

                $info = $file->move(ROOT_PATH . '/public' . DS . 'Uploader/User');
                if($info){
                    $imgUrl = '/Uploader/User/'.$info->getSaveName();
                    $Arr["portrait"] = $imgUrl;
                }else{
                    $this->error("上传头像失败");
                }
            }

            $Arr["nickname"] = $map["nickname"];
            if($map['old_pwd']&&$map['new_pwd'])
            {
            	$pas_salt = getRandChar(6);
            	$Arr["password"] = getMd5Pas($map["new_pwd"], $pas_salt);
                $Arr["pwd_code"] = $pas_salt;
            }
            if(!empty($map['pay_pwd'])){
            	$pas_salt = getRandChar(6);
            	$Arr["pay_pwd"] = getMd5Pas($map["pay_pwd"], $pas_salt);
                $Arr["pay_code"] = $pas_salt;
            }
            Db::name("User")->where("id",$this->userInfo["id"])->update($Arr);
            $this->redirect('User/index');
        }

        $Arr = app_model("index","user")->getuserInfo($this->userInfo["id"],"a.id,a.nickname,a.account,a.phone,a.portrait,a.pay_pwd,l.level");
        $Arr["is_pay"] = $Arr['pay_pwd']?'1':'0';
        $Arr['no_pay'] = getRandChar(6);

        $this->titleName = "修改个人信息";
        $this->assign("Arr",$Arr);
        return $this->fetch();
    }

    //查看密码输入是否正确
    function find_paw(){
    	if( request()->isAjax() ){
    		$old_pwd = input('post.old_pwd');
            
    		$Arr = Db::name("User")->where("id",$this->userInfo["id"] )->field('password,pwd_code')->find();

    		$old_pwd = getMd5Pas($old_pwd, $Arr['pwd_code']);

            if($old_pwd == $Arr['password']){
                $retArr = [
                    "result" => 0,
                    "msg" => "密码正确",
                ];
            }else{
                $retArr = [
                    "result" => 1001,
                    "msg" => "密码错误",
                ];
            }
            $this->json_return($retArr);
    	}
    }

    public function address(){
        $list = Db::name("Address")->where(array("status"=>["gt","0"],"user_id"=>$this->userInfo["id"]))->select();
        foreach($list as $k=>$v){
            $list[$k]["address"] = str_replace(",","",$v["address"]);
        }
        $this->assign("list",$list);
        $this->titleName = "收货地址";
        return $this->fetch();
    } 

    //添加地址
    public function add_address(){

        $headName = "添加地址";
        $this->titleName = "添加地址";

        $jumpUrl = $_SERVER['HTTP_REFERER'];

        if( request()->isPost() ){


            $data = input("post.");
            if( empty($data["province"]) || empty($data["city"]) || empty($data["town"]) ){

                $this->error("请选择地址");
            }

            $Arr["address"] = $data["province"].",".$data["city"].",".$data["town"].",".$data["address"];
            $Arr["id"] = $data["id"];
            $Arr["user_id"] = $this->userInfo["id"];
            $Arr["name"] = $data["name"];
            $Arr["phone"] = $data["phone"];
            $Arr["zip_code"] = $data["zip_code"];
            $Arr["time"] = time();
            $Arr["status"] = 1;
            //print_r($data);exit;
            //$id = Db::name("address")->insertGetId($Arr,false,true);
            $id = app_model("index","Address")->add_up_data($Arr);
            if( $id ){
                if( isset($data["jumpUrl"]) && !empty($data["jumpUrl"]) ){
                    $url = $data['jumpUrl'];
                    Header("Location: $url");
                    exit();
                }
                $this->redirect('User/address');
            }else{
                $this->error("编辑地址失败");
            }
        }else{

            $aid = input("param.aid");
            if( !empty($aid) ){

                $headName = "修改地址";
                $this->titleName = "修改地址";

                $Arr = Db::name("address")->where("id",$aid)->find();
                if( empty($Arr) ){
                    $this->redirect('User/address_list');
                }
                //print_r($Arr);exit;
                $ress = explode(",",$Arr["address"]);

                $ress = array_filter($ress);

                $Arr["province"] = $ress[0];
                $Arr["city"] = $ress[1];
                $Arr["town"] = $ress[2];
                $Arr["address"] = $ress[3];

                $this->assign("Arr",$Arr);
            }
        }
        $this->assign("jumpUrl",$jumpUrl);
        $this->assign("headName",$headName);
        return $this->fetch();
    }


    //地址列表
    public function del_address(){

        if( request()->isAjax() ){

            $id = input("post.id");
            if( empty($id) ){
                $retArr = [
                    "result" => 1,
                    "msg" => "删除地址失败",
                ];
                $this->dataError = "uid=".$this->userInfo["id"]."|".json_encode($id);
                $this->json_return($retArr);
            }
            $pid = Db::name("Address")->where("id",$id)->update(array("status"=>0));
            if( $pid ){
                $retArr = [
                    "result" => 0,
                    "msg" => "删除成功",
                ];
            }else{
                $retArr = [
                    "result" => 0,
                    "msg" => "删除失败",
                ];
                $this->dataError = "uid=".$this->userInfo["id"]."|".json_encode($id);
            }
            $this->json_return($retArr);
        }

    }
}